
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test_demo/lottery/simple_lottery_controller.dart';
import 'package:flutter_test_demo/lottery/simple_lottery_widget.dart';

class LotteryPage extends StatefulWidget {


  @override
  _LotteryPageState createState() => _LotteryPageState();
}

class _LotteryPageState extends State<LotteryPage> {
  late SimpleLotteryController simpleLotteryController;
  List<String> rewardsList = [];

  @override
  void initState() {
    rewardsList
      ..add("images/gift1.jpg")
      ..add("images/gift2.jpg")
      ..add("images/gift1.jpg")
      ..add("images/gift2.jpg")
      ..add("images/gift1.jpg")
      ..add("images/gift2.jpg")
      ..add("images/gift1.jpg")
      ..add("images/gift2.jpg");
    simpleLotteryController = SimpleLotteryController(rewardsList: rewardsList);
    simpleLotteryController.addListener(() {
      print(simpleLotteryController.value.toString());
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('抽奖'),
      ),
      body: Column(
        children: <Widget>[
          TextButton(
              child: Text("开始抽奖"),
              onPressed: () {
                simpleLotteryController.start(
                  start: 2,
                    target: 3,
                    duration: Duration(milliseconds: 2500),
                    repeatRound: 5);
              }),
          Center(
            child: SimpleLotteryWidget(
              controller: simpleLotteryController,
            ),
          ),
          TextButton(
              onPressed: () {
                _resetRewardList();
              },
              child: Text('Reset'))
        ],
      ),
    );
  }

  /// 当抽奖结束后，变更抽奖列表
  void _resetRewardList() {
    setState(() {
      rewardsList
        ..clear()
        ..add("images/gift3.jpg")
        ..add("images/gift2.jpg")
        ..add("images/gift3.jpg")
        ..add("images/gift2.jpg")
        ..add("images/gift3.jpg")
        ..add("images/gift2.jpg")
        ..add("images/gift3.jpg")
        ..add("images/gift2.jpg");
      simpleLotteryController =
          SimpleLotteryController(rewardsList: rewardsList);
    });
  }
}